<template>
  <el-dialog
    :close-on-click-modal="false"
    :title="!dataForm.id ? '新增' : '修改'"
    :visible.sync="visible"
  >
    <el-form ref="dataForm" :model="dataForm" :rules="dataRule" label-width="80px" @keyup.enter.native="dataFormSubmit()">
      <el-form-item label="父机构ID" prop="parentId">
        <el-input v-model="dataForm.parentId" placeholder="父机构ID" />
      </el-form-item>
      <el-form-item label="机构/部门名称" prop="departName">
        <el-input v-model="dataForm.departName" placeholder="机构/部门名称" />
      </el-form-item>
      <el-form-item label="英文名" prop="departNameEn">
        <el-input v-model="dataForm.departNameEn" placeholder="英文名" />
      </el-form-item>
      <el-form-item label="缩写" prop="departNameAbbr">
        <el-input v-model="dataForm.departNameAbbr" placeholder="缩写" />
      </el-form-item>
      <el-form-item label="排序" prop="departOrder">
        <el-input v-model="dataForm.departOrder" placeholder="排序" />
      </el-form-item>
      <el-form-item label="描述" prop="description">
        <el-input v-model="dataForm.description" placeholder="描述" />
      </el-form-item>
      <el-form-item label="机构类别 1组织机构，2岗位" prop="orgCategory">
        <el-input v-model="dataForm.orgCategory" placeholder="机构类别 1组织机构，2岗位" />
      </el-form-item>
      <el-form-item label="机构类型 1一级部门 2子部门" prop="orgType">
        <el-input v-model="dataForm.orgType" placeholder="机构类型 1一级部门 2子部门" />
      </el-form-item>
      <el-form-item label="机构编码" prop="orgCode">
        <el-input v-model="dataForm.orgCode" placeholder="机构编码" />
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="dataForm.mobile" placeholder="手机号" />
      </el-form-item>
      <el-form-item label="传真" prop="fax">
        <el-input v-model="dataForm.fax" placeholder="传真" />
      </el-form-item>
      <el-form-item label="地址" prop="address">
        <el-input v-model="dataForm.address" placeholder="地址" />
      </el-form-item>
      <el-form-item label="备注" prop="memo">
        <el-input v-model="dataForm.memo" placeholder="备注" />
      </el-form-item>
      <el-form-item label="状态（1启用，0不启用）" prop="status">
        <el-input v-model="dataForm.status" placeholder="状态（1启用，0不启用）" />
      </el-form-item>
      <el-form-item label="删除状态（0，正常，1已删除）" prop="delFlag">
        <el-input v-model="dataForm.delFlag" placeholder="删除状态（0，正常，1已删除）" />
      </el-form-item>
      <el-form-item label="创建人" prop="createBy">
        <el-input v-model="dataForm.createBy" placeholder="创建人" />
      </el-form-item>
      <el-form-item label="创建日期" prop="createTime">
        <el-input v-model="dataForm.createTime" placeholder="创建日期" />
      </el-form-item>
      <el-form-item label="更新人" prop="updateBy">
        <el-input v-model="dataForm.updateBy" placeholder="更新人" />
      </el-form-item>
      <el-form-item label="更新日期" prop="updateTime">
        <el-input v-model="dataForm.updateTime" placeholder="更新日期" />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        parentId: '',
        departName: '',
        departNameEn: '',
        departNameAbbr: '',
        departOrder: '',
        description: '',
        orgCategory: '',
        orgType: '',
        orgCode: '',
        mobile: '',
        fax: '',
        address: '',
        memo: '',
        status: '',
        delFlag: '',
        createBy: '',
        createTime: '',
        updateBy: '',
        updateTime: ''
      },
      dataRule: {
        parentId: [
          { required: true, message: '父机构ID不能为空', trigger: 'blur' }
        ],
        departName: [
          { required: true, message: '机构/部门名称不能为空', trigger: 'blur' }
        ],
        departNameEn: [
          { required: true, message: '英文名不能为空', trigger: 'blur' }
        ],
        departNameAbbr: [
          { required: true, message: '缩写不能为空', trigger: 'blur' }
        ],
        departOrder: [
          { required: true, message: '排序不能为空', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '描述不能为空', trigger: 'blur' }
        ],
        orgCategory: [
          { required: true, message: '机构类别 1组织机构，2岗位不能为空', trigger: 'blur' }
        ],
        orgType: [
          { required: true, message: '机构类型 1一级部门 2子部门不能为空', trigger: 'blur' }
        ],
        orgCode: [
          { required: true, message: '机构编码不能为空', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' }
        ],
        fax: [
          { required: true, message: '传真不能为空', trigger: 'blur' }
        ],
        address: [
          { required: true, message: '地址不能为空', trigger: 'blur' }
        ],
        memo: [
          { required: true, message: '备注不能为空', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '状态（1启用，0不启用）不能为空', trigger: 'blur' }
        ],
        delFlag: [
          { required: true, message: '删除状态（0，正常，1已删除）不能为空', trigger: 'blur' }
        ],
        createBy: [
          { required: true, message: '创建人不能为空', trigger: 'blur' }
        ],
        createTime: [
          { required: true, message: '创建日期不能为空', trigger: 'blur' }
        ],
        updateBy: [
          { required: true, message: '更新人不能为空', trigger: 'blur' }
        ],
        updateTime: [
          { required: true, message: '更新日期不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/system/depart/info/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.parentId = data.depart.parentId
              this.dataForm.departName = data.depart.departName
              this.dataForm.departNameEn = data.depart.departNameEn
              this.dataForm.departNameAbbr = data.depart.departNameAbbr
              this.dataForm.departOrder = data.depart.departOrder
              this.dataForm.description = data.depart.description
              this.dataForm.orgCategory = data.depart.orgCategory
              this.dataForm.orgType = data.depart.orgType
              this.dataForm.orgCode = data.depart.orgCode
              this.dataForm.mobile = data.depart.mobile
              this.dataForm.fax = data.depart.fax
              this.dataForm.address = data.depart.address
              this.dataForm.memo = data.depart.memo
              this.dataForm.status = data.depart.status
              this.dataForm.delFlag = data.depart.delFlag
              this.dataForm.createBy = data.depart.createBy
              this.dataForm.createTime = data.depart.createTime
              this.dataForm.updateBy = data.depart.updateBy
              this.dataForm.updateTime = data.depart.updateTime
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/system/depart/${!this.dataForm.id ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'id': this.dataForm.id || undefined,
              'parentId': this.dataForm.parentId,
              'departName': this.dataForm.departName,
              'departNameEn': this.dataForm.departNameEn,
              'departNameAbbr': this.dataForm.departNameAbbr,
              'departOrder': this.dataForm.departOrder,
              'description': this.dataForm.description,
              'orgCategory': this.dataForm.orgCategory,
              'orgType': this.dataForm.orgType,
              'orgCode': this.dataForm.orgCode,
              'mobile': this.dataForm.mobile,
              'fax': this.dataForm.fax,
              'address': this.dataForm.address,
              'memo': this.dataForm.memo,
              'status': this.dataForm.status,
              'delFlag': this.dataForm.delFlag,
              'createBy': this.dataForm.createBy,
              'createTime': this.dataForm.createTime,
              'updateBy': this.dataForm.updateBy,
              'updateTime': this.dataForm.updateTime
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
